# coding:utf8

from appserver import db_store
from sqlalchemy import or_
from models.entity.department import Department
from models.entity.tree_model_base import TreeModelBase

"""
机构数据模型
"""


class Organization(TreeModelBase):
    __tablename__ = "organizations"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'机构编号')
    name = db_store.Column(db_store.String(64), nullable=False, unique=True, comment=u'机构名称')
    code = db_store.Column(db_store.String(32), nullable=False, unique=True, comment=u"机构编码")
    description = db_store.Column(db_store.String(255), comment=u"描述")
    parent_id = db_store.Column(db_store.Integer, comment=u"父ID")
    path = db_store.Column(db_store.String(255), comment=u"路径")
    depth = db_store.Column(db_store.Integer, comment=u"深度")
    child_num = db_store.Column(db_store.Integer, comment=u"子类数量",default=0)
    dis_order = db_store.Column(db_store.Integer, comment=u"排序")
    status = db_store.Column(db_store.Integer, comment=u"状态")  # 0-正常 1-禁用
    chinese_name=db_store.Column(db_store.String(64),comment=u"中文名称")
    english_name=db_store.Column(db_store.String(64),comment=u"英文名称")
    set_up_time=db_store.Column(db_store.DateTime(),comment=u"成立时间")
    person_in_charge=db_store.Column(db_store.String(64),comment=u"负责人")
    phone_number=db_store.Column(db_store.String(32),comment=u"电话号码")
    email=db_store.Column(db_store.String(32),comment=u"电子邮箱")
    fax=db_store.Column(db_store.String(32),comment=u"传真号码")
    location=db_store.Column(db_store.String(128),comment=u"机构地点")
    zip_code=db_store.Column(db_store.String(32),comment=u"邮政编码")
    web=db_store.Column(db_store.String(128),comment=u"机构官网")
    management_scope=db_store.Column(db_store.String(512),comment=u"经营范围")
    character=db_store.Column(db_store.String(64),comment=u"机构性质")

    def __repr__(self):
        return "<Organization %s>" % self.name

    def to_dict(self):
        rdic={}
        rdic["id"] = self.id
        rdic["name"] = self.name
        rdic["code"] = self.code
        rdic["description"] = self.description
        rdic["ParentId"] = self.parent_id
        rdic["path"] = self.path
        rdic["Level"] = self.depth
        rdic["child_num"] = self.child_num
        rdic["dis_order"] = self.dis_order
        rdic["status"] = self.status
        rdic["chinese_name"] = self.chinese_name
        rdic["english_name"] = self.english_name
        rdic["set_up_time"] = self.set_up_time.strftime("%F")
        rdic["person_in_charge"] = self.person_in_charge
        rdic["phone_number"] = self.phone_number
        rdic["email"] = self.email
        rdic["fax"] = self.fax
        rdic["location"] = self.location
        rdic["zip_code"] = self.zip_code
        rdic["web"] = self.web
        rdic["management_scope"] = self.management_scope
        rdic["character"] = self.character
        return rdic

    @staticmethod
    def get_organization_by_department_id(department_id):
        return Organization.query.filter(Organization.id==Department().get_entity_by_id(department_id).org_id).first()

    @staticmethod
    def get_organization_by_name(name):
        return Organization.query.filter_by(name=name).first()

    @staticmethod
    def get_organization_by_id(id):
        return Organization.query.filter_by(id=id).first()

    @staticmethod
    def get_organization_by_code(code):
        return Organization.query.filter_by(code=code).first()

    @staticmethod
    def get_first_level_organizations():
        # 获取所有第一层的机构
        return Organization.query.filter_by(parent_id=0).all()

    @staticmethod
    def get_organizations_by_department(the_department):
        # 通过部门实例获取机构实例
        return Organization.query.filter_by(id=the_department.org_id).first()
    
    @staticmethod
    def search(query_word):
        search_list=Organization.query.filter(or_(Organization.name.like("%"+query_word+"%"),Organization.code.like("%"+query_word+"%"),
                                                                                Organization.description.like("%"+query_word+"%"),Organization.chinese_name.like("%"+query_word+"%"),
                                                                                Organization.person_in_charge.like("%"+query_word+"%"),Organization.phone_number.like("%"+query_word+"%"),
                                                                                Organization.phone_number.like("%"+query_word+"%"),Organization.email.like("%"+query_word+"%"),
                                                                                Organization.fax.like("%"+query_word+"%"),Organization.location.like("%"+query_word+"%"),
                                                                                Organization.zip_code.like("%"+query_word+"%"),Organization.web.like("%"+query_word+"%"),
                                                                                Organization.management_scope.like("%"+query_word+"%"),Organization.character.like("%"+query_word+"%"))).order_by("dis_order").all()
        tlist=[]
        for item in search_list:
             tlist.append(item.to_dict())
        rdic={
                        "success":1,
                        "error_code":None,
                        "error_msg":None,
                        "data":tlist
                }
        return rdic
